//
//  SafeCentreViewController.m
//  NiuBanJin
//
//  Created by zf on 15/10/10.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "SafeCentreViewController.h"
#import "AuthenticationNameViewControllrer.h"
#import "BankCardManageViewController.h"
#import "GestureViewController.h"
#import "ModifyRollPWDViewController.h"
#import "WebViewController.h"
#import "AppDelegate.h"
#import "RetrievingPWDViewController.h"
#import "LoginViewController.h"
#import "IDCardAuthenticationViewController.h"
#import "BankCardBindingViewController.h"
@interface SafeCentreViewController ()<UITableViewDataSource , UITableViewDelegate  , UIAlertViewDelegate>
@property (nonatomic , strong) NSArray *array_moreArr1;
@property (nonatomic , strong) NSArray *array_moreArr2;
@property (nonatomic , strong) UITableView *tableview; //show data


@end

@implementation SafeCentreViewController


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    // old crash reason: 删除了一个不存在的cell
    
    if ([UsefulUtil isChinese:GETCURRENTUSERINOBYPRAMETERES(user_userRealName)]) {
        
        [self.tableview reloadData];
    }
    
    if ([GETCURRENTUSERINOBYPRAMETERES(user_isCardBinding) boolValue]) {
        
        [self.tableview reloadData];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    self.navigationItem.title = @"安全中心";
    self.view.backgroundColor = [UsefulUtil colorWithHexString:@"#989898"];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    self.tableview = tableView;
    tableView.tag = 400;
    
    [tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    UIView *view_footView= [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, CGRectGetMaxY(tableView.frame), SCREEN_WIDTH, MULTIPLYHEIGHT(40.0))];
    view_footView.backgroundColor = [UsefulUtil colorWithHexString:@"#ededed"];
        [tableView setTableFooterView:view_footView];
    UIButton *button_signOut = [UIButton buttonWithType:UIButtonTypeSystem];
    button_signOut.tintColor = [UIColor whiteColor];
    [self.view addSubview:button_signOut];
    
    button_signOut.frame = CGRectMake(MULTIPLYWIDTH(15),MULTIPLYHEIGHT(105 + 45 * 7) , SCREEN_WIDTH - MULTIPLYWIDTH(30), MULTIPLYHEIGHT(45));
    
    
       [button_signOut addTarget:self action:@selector(signOut) forControlEvents:UIControlEventTouchUpInside];
    button_signOut.backgroundColor = REDCOLOR;
    [button_signOut setTitle:@"退出登录" forState:UIControlStateNormal];
  
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
    self.array_moreArr1 = @[@"用户名" , @"实名认证" , @"银行卡管理"];
    self.array_moreArr2 = @[ @"手势密码管理" , @"修改转出密码" , @"找回转出密码" ];
   
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction)];
    [self.view addGestureRecognizer:swipeGesture];
   
}
#pragma mark -swipeGestureAction
-(void)swipeGestureAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - signOut 退出
-(void)signOut{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"退出登录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
 
    
}
#pragma mark alertView
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        
    }
    if (buttonIndex == 1) {
//        NSDictionary *dic_logOut = @{@"userId" : GETCURRENTUSERINOBYPRAMETERES(user_userId) , @"token" : GETCURRENTUSERINOBYPRAMETERES(user_userToken)};
//        [[NETWorkingUtil sharedNETWorking] zzjrMore_logoutByParmeteres:dic_logOut transferResultUsingBlock:^(id parsedData, NSError *error) {
//            if (!error) {
//                
//                if ([parsedData[@"status"] integerValue] == 0) {
        
                    [[UserManager sharedUserManager] logOut];
                    
                    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[LoginViewController new]];
                    
                    [self presentViewController:nav animated:YES completion:nil];
//                }
//            }else {
//                [UsefulUtil showAlertView:@"遇到网络问题,请稍后重试!"];
//            }
//        }];
//        
    }
}
#pragma mark - heightForHeaderInSection
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat height = 0  ;
    if (section == 0) {
        height = 15;
    }
    if (section == 1) {
        height = 15;
    }
    if (section == 2) {
        height = 30;
    }

    return height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    CGFloat height = 0.0 ;
    if (section == 2) {
        height = 30;
    }
    return height;
}
#pragma mark - numberOfSectionsInTableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}
#pragma mark - numberOfRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger  number = 0;
    if (section == 0) {
        
        number = self.array_moreArr1.count;
        
    }
    if (section == 1) {
        number = self.array_moreArr2.count;
        
    }
        return number;
}
#pragma mark - cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        
    }
    
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textColor = [UsefulUtil colorWithHexString:@"#434343"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - MULTIPLYWIDTH(240.0), MULTIPLYHEIGHT(5.0), MULTIPLYWIDTH(200.0), MULTIPLYHEIGHT(30.0))];
    label.font = [UIFont systemFontOfSize:15.0];
    label.textColor = [UsefulUtil colorWithHexString:@"#989898"];
    label.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:label];
    
    if (indexPath.section == 0) {
        
        cell.textLabel.text = [self.array_moreArr1 objectAtIndex:indexPath.row];
        //用户名取值
        if (indexPath.row == 0) {
            
            if ( [UsefulUtil isChinese:GETCURRENTUSERINOBYPRAMETERES(user_userRealName)]) { //这个值会默认返回来电话号码 ,so ,judge the result is chinse or not
              
                NSString *name_str = [GETCURRENTUSERINOBYPRAMETERES(user_userRealName) substringWithRange:NSMakeRange(1, GETCURRENTUSERINOBYPRAMETERES(user_userRealName).length -1)];
                label.text = [NSString stringWithFormat:@"*%@" , name_str];
            }else {
                
                label.text = GETCURRENTUSERINOBYPRAMETERES(user_userName);
            }
            
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        //实名认证取值
        if (indexPath.row == 1) {
          
            if ([UserManager sharedUserManager].isAuthentication == YES) {
                
                label.text = @"已经认证";
            }else {
                
                label.text = @"未认证";
            }
        }
        //银行卡管理取值
        if (indexPath.row == 2) {
            if ([UserManager sharedUserManager].isCardBinding == YES) {
                
                label.text = @"已经绑卡";
                
            }else {
                
                label.text = @"未绑卡";
                
            }
        }
    }
    if (indexPath.section == 1) {
        
        cell.textLabel.text = [self.array_moreArr2 objectAtIndex:indexPath.row];
    }
    
    return cell;
}
#pragma mark - didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        if ([UserManager sharedUserManager].isAuthentication == YES) {
            
            AuthenticationNameViewControllrer *AUVC = [[AuthenticationNameViewControllrer alloc]init];
            [self.navigationController pushViewController:AUVC animated:YES];
        }else {
            IDCardAuthenticationViewController *IDVC = [[IDCardAuthenticationViewController alloc]init];
            IDVC.Type = 1;
            [self.navigationController pushViewController:IDVC animated:YES];

        }
       
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        
        if ([UserManager sharedUserManager].isAuthentication == YES) {
            
            if ([UserManager sharedUserManager].isCardBinding == YES) {
                
                BankCardManageViewController *BVC = [[BankCardManageViewController alloc]init];
                [self.navigationController pushViewController:BVC animated:YES];
                
            }else {
                
                BankCardBindingViewController *bankBindVC = [[BankCardBindingViewController alloc]init];
                [self.navigationController pushViewController:bankBindVC animated:YES];
            }

        }else {
            IDCardAuthenticationViewController *IDVC = [[IDCardAuthenticationViewController alloc]init];
            IDVC.Type = 1;
            [self.navigationController pushViewController:IDVC animated:YES];
            
        }

        
       
    }
    if (indexPath.section == 1 &&  indexPath.row == 0) {
        GestureViewController *gestureVC = [[GestureViewController alloc]init];
        [self.navigationController pushViewController:gestureVC animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 1 ) {
        
        if ([UserManager sharedUserManager].isAuthentication == YES) {
            
            ModifyRollPWDViewController *modifyVC = [[ModifyRollPWDViewController alloc]init];
            modifyVC.index = indexPath.row;
            [self.navigationController pushViewController:modifyVC animated:YES];
            
        }else {
            IDCardAuthenticationViewController *IDVC = [[IDCardAuthenticationViewController alloc]init];
            IDVC.Type = 1;
            [self.navigationController pushViewController:IDVC animated:YES];
            
        }
       
    }
    if (indexPath.section == 1 &&  indexPath.row == 2 ) {
        
        if ([UserManager sharedUserManager].isAuthentication == YES) {
            
            RetrievingPWDViewController *RetrieVC = [[RetrievingPWDViewController alloc]init];
            
            [self.navigationController pushViewController:RetrieVC animated:YES];
            
        }else {
            IDCardAuthenticationViewController *IDVC = [[IDCardAuthenticationViewController alloc]init];
            IDVC.Type = 1;
            [self.navigationController pushViewController:IDVC animated:YES];
            
        }

       
    }

        
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
